package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {

        //将套餐插入setmeal表中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        //将套餐关联的菜品插入setmeal_dish表中
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectByPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectByPage(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐及关联菜品
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {

        //根据id查询setmeal表格
        SetmealVO setmealVO = setmealMapper.selectById(id);
        //根据id查询setmeal_dish表格
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {

        //删除setmeal表数据
        setmealMapper.deleteBatch(ids);

        //删除setmeal_dish表关联数据
        setmealDishMapper.deleteBatch(ids);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void udpate(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //更新setmeal表格
        setmealMapper.update(setmeal);

        //删除setmeal_dish表格关联数据
        List<Long> setmealId = new ArrayList<>();
        setmealId.add(setmealDTO.getId());
        setmealDishMapper.deleteBatch(setmealId);

        //为字段setmealId赋值
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() != 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
        }

        //将更新的菜品插入setmeal_dish表格
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 启用或禁用套餐
     * @param id
     */
    @Override
    public void disableOrEnable(Integer status, Long id) {

        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();

        setmealMapper.update(setmeal);
    }

    /**
     * 根据种类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> selectByCategoryId(Integer categoryId) {

        return setmealMapper.selectByCategoryId(categoryId);
    }

    /**
     * 根据套餐id查询菜品item
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> selectDishBySetmealId(Integer id) {

        return setmealMapper.selectDishBySetmealId(id);
    }


}
